//index.js
//获取应用实例
const plugin = requirePlugin("WechatSI")
const app = getApp()

Page({
  data: {
    show_tip: false,
    active: true,
    res_title: '推荐内容',
    paclce_txt: '搜索学校名称',
    msgList: [],
    page: 1,
    ship: 0,
    limit: 20,
    tip_type: 1,
    tips_title: '学校',
    tab: "list",
    compid__3: true,
    initLoading: false,
    schoolList: [],
    default_img: 'http://bmobfile.twho.top/2020/04/17/2a60bcae4049592f80a057b06e205801.png',
    today: '',
    moretip: `<span class="jiaos" >阅读全文</span>`,
    unitId: '',
    nomore: false,
    flag: !1,
    schoolInfo: '',
  },
  showimg(e) {
    let img = e.currentTarget.dataset.img;
    if (img) {
      wx.previewImage({
        current: img,
        urls: [img]
      });
    }
  },

  changTip(e) {
    let type = e.currentTarget.dataset.type;
    let tips_title = '学校和专业'
    let paclce_txt = '搜索学校-专业'
    if (type == 1) {
      tips_title = "学校";
      paclce_txt = '搜索学校名称'
    } else if (type == 2) {
      tips_title = "专业"
      paclce_txt = '搜索专业名称'
    }
    console.log(type)
    console.log(tips_title)
    this.setData({
      tip_type: type,
      tips_title: tips_title,
      show_tip: false,
      paclce_txt: paclce_txt,
      title: ''
    })
  },
  showTip() {
    this.setData({
      show_tip: true
    })
  },
  speek(e) {
    let info = e.currentTarget.dataset.info;

    if (info) {
      wx.showToast({
        title: '准备开始朗读~',
      })

      this.sayVoice(info)
    }

  },
  sayVoice(e) {
    let info = this.delHtmlTag(e)
    info = info.replace(/&nbsp;/ig, "");
    plugin.textToSpeech({
      lang: 'zh_CN',
      content: info || '没有找到内容',
      success: resTrans => {
        wx.playBackgroundAudio({
          dataUrl: resTrans.filename,
          title: '',
        })
      },
      fail: err => {
        console.log(err)
        wx.showToast({
          title: '文字太长,播放失败',
          icon: 'none'
        })
      }
    })

  },
  delHtmlTag(str) {
    return str.replace(/<[^>]*>|/g, "") //去掉所有的html标记
  },

  todetail(e) {

    let aid = e.currentTarget.dataset.aid;
    if (aid) {
      wx.navigateTo({
        url: "/pages/detail/index?tid=" + aid,
        success: function (t) {

        }
      });
    }
  },
  onLoad: function (option) {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    if (option.tid) {
      wx.navigateTo({
        url: '../detail/index?tid=' + option.tid
      })
    }
    this.getRandList()
    this.getNotice()

  },

  //获取学校列表
  getSchool() {
    let that = this
    let ship = this.data.ship
    let limit = this.data.limit
    let school = this.data.pschool,
      subject = this.data.psubject;
    let where = {}
    if (subject) {
      where.subject = {
        $regex: '.*' + subject,
        $options: 'i'
      }
    }
    if (school) {
      where.school = {
        $regex: '.*' + school
      }
    }
    let params = {
      limit,
      ship,
      where
    }
    wx.isSDK.table("school", params).then(res => {

      if (res.code === 200) {
        let list = that.data.schoolList.concat(res.data);
        this.setData({
          schoolList: list,

        });
      }
    })

  },
  //随机获取学校数据
  getRandList() {
    let params = {
      sample: {
        size: 10, //随机取的条数
      }
    }
    wx.isSDK.table("school", params).then(res => {
      if (res.code === 200) {
        let list = this.data.schoolList.concat(res.data);
        this.setData({
          schoolList: list,
        });
      }
    })

  },
  //获取公告
  getNotice() {
    let params = {
      where: {
        show: true
      }
    }
    wx.isSDK.table("notice", params).then(res => {
      if (res.code === 200) {
        this.setData({
          msgList: res.data
        })
      }
    })
  },

  timeFormate() {
    var myDate = new Date();
    var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var month = myDate.getMonth() + 1;
    var strDate = myDate.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }

    let daystr = year + '-' + month + '-' + strDate;
    this.setData({
      today: daystr
    })
  },

  goTop(e) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  onShareAppMessage: function (t) {
    var e = wx.getStorageSync("nickName"),
      o = "来看看学长学姐对学校说了什么,评价如何?";
    var a = t.target.dataset.voteid
    if ("button" === t.from && a) {

      return {
        title: o + "对大学de评论",
        path: "/pages/detail/index?tid=" + a,
        imageUrl: 'http://bmobfile.twho.top/2020/04/17/7281a79540e9edba808861891d77edf5.jpeg',
        success: function (t) {

        }
      };
    } else {
      return {
        title: "来看看学长学姐对学校说了什么,评价如何?快来窥探一下",
        path: "/pages/index/index",
        imageUrl: 'http://bmobfile.twho.top/2020/04/17/7281a79540e9edba808861891d77edf5.jpeg',
        success: function (t) {

        }
      }
    }
  },
  tips() {
    var a = this;
    wx.setStorageSync("flag", "1"), a.setData({
      flag: wx.getStorageSync("flag")
    });
  },
  onShow() {

  },
  formSubmit(e) {
    console.log(e)
    console.log(this.data.tip_type)
    let title = e.detail.value.title
    if (title) {
      this.setData({
        skip: 0
      })
      if (this.data.tip_type == 1) {
        this.initData()
        this.setData({
          pschool: title
        })
        this.getSchool()
      } else if (this.data.tip_type == 2) {
        this.initData()
        this.setData({
          psubject: title
        })
        this.getSchool()
      } else if (this.data.tip_type == 3) {
        let arr = title.split("-")
        if (arr.length == 2) {
          this.initData()
          this.setData({
            pschool: arr[0],
            psubject: arr[1]
          })
          this.getSchool()
        } else {
          wx.showToast({
            title: '输入格式不对',
            icon: 'none'
          })
        }
      }
    } else {

      this.initData()
      this.getSchool()
    }
  },
  initData() {
    this.setData({
      schoolList: [],
      skip: 0
    })
  },
  onReachBottom: function () {
    if (this.data.nomore) {
      wx.showToast({
        title: '没有更多数据~',
        icon: 'none'
      })
      return
    }
    wx.showToast({
      title: '加载中...',
      icon: 'none'
    })
    this.setData({
      ship: (this.data.ship + 1)
    })
    this.getSchool()

  },

})